# -*- coding:utf-8 -*-
# business/business_scenario.py

from sanic.log import logger
from sqlalchemy import select

from model.model_result import DataResult
from model.models import BusinessScenario
from business.common_func import get_busi_chain_by_busi_scenario_id


async def get_business_scenario(request):
    """
    获取业务场景列表
    """
    data_result = DataResult()
    session = request.ctx.session
    try:
        busi_scenario_list = []
        async with session.begin():
            query_busi_scenario = select(BusinessScenario).where(BusinessScenario.is_deleted == 0)
            result = await session.execute(query_busi_scenario)
            busi_scenario_objs = result.scalars().all()
            busi_scenario_list = [v.to_dict() for v in busi_scenario_objs]
            logger.debug(f"get business_scenario: {busi_scenario_list}")
        data_result.data["items"] = busi_scenario_list
        data_result.message = "获取业务场景列表成功"
    except Exception as ex:
        data_result.status = False
        data_result.code = 400
        data_result.message = "获取业务场景列表失败"
        logger.exception(ex)
    return data_result


async def get_business_scenario_detail_by_id(busi_scenario_id, request):
    """
    根据业务场景ID查询业务场景详情
    """
    if not busi_scenario_id:
        raise Exception('缺少业务场景ID(busi_scenario_id)参数值!')
    data_result = DataResult()
    session = request.ctx.session
    try:
        busi_scenario_info = {}
        async with session.begin():
            query_busi_scenario = select(
                BusinessScenario
            ).where(
                BusinessScenario.business_scenario_id == busi_scenario_id,
                BusinessScenario.is_deleted == 0
            )
            result = await session.execute(query_busi_scenario)
            busi_scenario_obj = result.scalar()
            busi_scenario_info = busi_scenario_obj.to_dict()
            logger.debug(f"get business_scenario info by id {busi_scenario_id}, res: {busi_scenario_info}")
        busi_scenario_info['app_sys_chain'] = await get_busi_chain_by_busi_scenario_id(session, busi_scenario_id)
        data_result.data = busi_scenario_info
        data_result.message = "根据业务场景ID查询业务场景详情成功"
    except Exception as ex:
        data_result.status = False
        data_result.code = 400
        data_result.message = "根据业务场景ID查询业务场景详情失败"
        logger.exception(ex)
    return data_result


async def query_business_scenario_like_name(busi_scenario_name, request):
    """
    根据业务场景名称模糊查询业务场景列表
    """
    data_result = DataResult()
    session = request.ctx.session
    try:
        busi_scenario_list = []
        async with session.begin():
            query_busi_scenario = select(
                BusinessScenario
            ).where(
                BusinessScenario.name.like(f"%{busi_scenario_name}%"),
                BusinessScenario.is_deleted == 0
            )
            result = await session.execute(query_busi_scenario)
            busi_scenario_objs = result.scalars().all()
            busi_scenario_list = [v.to_dict() for v in busi_scenario_objs]
            logger.debug(f"query business_scenario like name {busi_scenario_name}, res: {busi_scenario_list}")
        data_result.data = busi_scenario_list
        data_result.message = "根据业务场景名称模糊查询业务场景列表成功"
    except Exception as ex:
        data_result.status = False
        data_result.code = 400
        data_result.message = "根据业务场景名称模糊查询业务场景列表失败"
        logger.exception(ex)
    return data_result


async def query_business_scenario_by_main_busi_id(main_busi_id, request):
    """
    根据主体业务ID查询业务场景列表
    """
    if not main_busi_id:
        raise Exception('缺少主体业务ID(main_busi_id)参数值!')
    data_result = DataResult()
    session = request.ctx.session
    try:
        busi_scenario_list = []
        async with session.begin():
            query_busi_scenario = select(
                BusinessScenario
            ).where(
                BusinessScenario.main_business_id == main_busi_id,
                BusinessScenario.is_deleted == 0
            )
            result = await session.execute(query_busi_scenario)
            busi_scenario_objs = result.scalars().all()
            busi_scenario_list = [v.to_dict() for v in busi_scenario_objs]
            logger.debug(f"query business_scenario by main_busi_id {main_busi_id}, res: {busi_scenario_list}")
        data_result.data = busi_scenario_list
        data_result.message = "根据主体业务ID查询业务场景列表成功"
    except Exception as ex:
        data_result.status = False
        data_result.code = 400
        data_result.message = "根据主体业务ID查询业务场景列表失败"
        logger.exception(ex)
    return data_result
